﻿using System;
using System.Linq;
using System.Collections.Generic;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.RateParamTypes;

namespace Application.MainModule.RateParamTypeManagement
{
    public class RateParamTypeManagementService : IRateParamTypeManagementService, IDisposable
    {
        #region private properties

        private IRateParamTypeRepository RateParamTypeRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for RateParamTypeTypeservice.
        /// </summary>
        /// <param name="RateParamTypeRepository">RateParamTypeRepository dependency, usually this is resolved by IoC</param>
        public RateParamTypeManagementService(IRateParamTypeRepository RateParamTypeRepository)
        {
            if (RateParamTypeRepository == (IRateParamTypeRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.RateParamTypeRepository = RateParamTypeRepository;
        }

        #endregion

        #region IRateParamTypeTypeservice implementations

        public void AddRateParamType(RateParamType RateParamType)
        {
            IUnitOfWork unitOfWork = this.RateParamTypeRepository.UnitOfWork as IUnitOfWork;
            this.RateParamTypeRepository.Add(RateParamType);
            unitOfWork.Commit();
        }

        public void ChangeRateParamType(RateParamType RateParamType)
        {
            IUnitOfWork unitOfWork = this.RateParamTypeRepository.UnitOfWork as IUnitOfWork;
            this.RateParamTypeRepository.Modify(RateParamType);
            unitOfWork.CommitAndRefreshChanges();
        }

        public RateParamType FindRateParamTypeById(Guid RateParamTypeId)
        {
            RateParamTypeIdSpecification RateParamTypeIdSpecification = new RateParamTypeIdSpecification(RateParamTypeId);
            return this.RateParamTypeRepository.FindRateParamType(RateParamTypeIdSpecification);
        }

        public List<RateParamType> FindRateParamTypesByName(string name)
        {
            RateParamTypeNameSpecification RateParamTypeNameSpecification = new RateParamTypeNameSpecification(name);
            return this.RateParamTypeRepository.FindRateParamTypes(RateParamTypeNameSpecification).ToList<RateParamType>();
        }

        public void RemoveRateParamType(RateParamType RateParamType)
        {
            if (RateParamType == (RateParamType)null)
                throw new ArgumentNullException("RateParamType");

            IUnitOfWork unitOfWork = this.RateParamTypeRepository.UnitOfWork as IUnitOfWork;
            this.RateParamTypeRepository.Remove(RateParamType);
            unitOfWork.CommitAndRefreshChanges();

        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.RateParamTypeRepository != null
                &&
                this.RateParamTypeRepository.UnitOfWork != null)
            {
                this.RateParamTypeRepository.UnitOfWork.Dispose();
            }
        }

        #endregion
    }
}
